package com.eastfair.pay.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.echo.core.EchoService;
import com.eastfair.pay.dto.PaymentDTO;
import com.eastfair.pay.dto.TransactionOrderDTO;
import com.eastfair.pay.dto.TransactionOrderPageQuery;
import com.eastfair.pay.dto.TransactionOrderSaveDTO;
import com.eastfair.pay.dto.TransactionOrderUpdateDTO;
import com.eastfair.pay.entity.TransactionOrder;
import com.eastfair.pay.service.ChinaumsLogService;
import com.eastfair.pay.service.TransactionOrderService;
import com.eastfair.pay.vo.TransactionOrderVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 * 前端控制器
 * 交易订单
 * </p>
 *
 * @author fwl
 * @date 2023-03-13
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/transactionOrder")
@Api(value = "TransactionOrder", tags = "交易订单")
@PreAuth(replace = "pay:transactionOrder:")
public class TransactionOrderController extends SuperController<TransactionOrderService, Long, TransactionOrder, TransactionOrderPageQuery, TransactionOrderSaveDTO, TransactionOrderUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Resource
    private TransactionOrderService transactionOrderService;

    @Resource
    private ChinaumsLogService chinaumsLogService;

    @Override
    public void handlerResult(IPage<TransactionOrder> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<TransactionOrderPageQuery> params) {
        TransactionOrderPageQuery pageQuery = params.getModel();

    }


    /**
     * 保存交易订单
     *
     * @param transactionOrderDTO
     * @return
     */
    @PostMapping("/save")
    public R<Boolean> save(@RequestBody TransactionOrderDTO transactionOrderDTO) {
        try {
            transactionOrderService.save(transactionOrderDTO);
            return R.success();
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 批量保存订单
     *
     * @param transactionOrderList
     * @return
     */
    @PostMapping("/save/batch")
    public R<Boolean> batchSave(@RequestBody List<TransactionOrderDTO> transactionOrderList) {
        try {
            transactionOrderService.batchSave(transactionOrderList);
            return R.success();
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 更新交易订单
     *
     * @param transactionOrderDTO
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody TransactionOrderDTO transactionOrderDTO) {
        try {
            transactionOrderService.updateById(transactionOrderDTO);
            return R.success();
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }

    @PostMapping("/update/orderStatus")
    public R updateOrderStatus(@RequestBody TransactionOrderDTO transactionOrderDTO) {
        try {
            transactionOrderService.updateOrderStatus(transactionOrderDTO);
            return R.success();
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 取消交易订单
     *
     * @param orderNum
     * @return
     */
    @GetMapping("/cancel")
    public R cancel(@RequestParam("orderNum") String orderNum) {
        try {
            transactionOrderService.cancel(orderNum);
            return R.success();
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * @return com.eastfair.core.base.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page < com.eastfair.pay.vo.TransactionOrderVO>>
     * @Author fwl
     * @Description //分页检索
     * @Date 17:04 2023/3/13
     * @Param [params]
     **/
    @PostMapping("/queryPage")
    public R<Page<TransactionOrderVO>> queryPage(@RequestBody @Validated PageParams<TransactionOrderPageQuery> params) {

        try {
            return R.success(baseService.queryList(params));
        } catch (BizException biz) {
            return R.fail(biz);
        }

    }


    /**
     * 在线支付 同步订单状态和银联交易流水号
     *
     * @param dto
     * @return
     */
    @PostMapping("/online")
    public R<Boolean> onlinePayment(@RequestBody PaymentDTO dto) {
        transactionOrderService.onlinePayment(dto, null);
        return R.success(true);
    }

    /**
     * 获取交易订单详情
     *
     * @param id
     * @param orderType
     * @return
     */
    @GetMapping("queryDetails/{orderType}/{id}")
    public R<List<TransactionOrderVO>> queryTransactionOrderDetails(@PathVariable("id") Long id, @PathVariable("orderType") String orderType) {
        try {
            List<TransactionOrder> transactionOrders = transactionOrderService.queryTransactionOrderDetails(id, orderType);
            List<TransactionOrderVO> transactionOrderVOS = ConvertUtil.convertList(transactionOrders, TransactionOrderVO.class);
            return R.success(transactionOrderVOS);
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 获取交易订单详情
     *
     * @return
     */
    @GetMapping("getDetails/{orderNum}")
    public R<TransactionOrderVO> queryOrderDetailsByOrderNum(@PathVariable("orderNum") String orderNum) {
        return R.success(transactionOrderService.queryOrderDetailsByOrderNum(orderNum));

    }

    /**
     * 核实支付
     *
     * @param id
     * @return
     */
    @GetMapping("check/order/{id}")
    public R checkPayStatus(@PathVariable("id") Long id) {
        try {
            transactionOrderService.checkPayStatus(id);
            return R.success();
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }


    @PostMapping("/syncOrderToJY")
    public R executeSyncOrderToJY(@RequestBody List<String> orderNumList) {
        try {
            transactionOrderService.executeSyncOrderToJY(orderNumList);
            return R.success();
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }

}
